package ui

import org.apache.wicket.markup.html.WebPage
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import dominio.Jugador
import applicationModel.Fila
import org.apache.wicket.markup.html.list.ListItem
import org.apache.wicket.markup.html.form.Form
import applicationModel.Plantar
import org.apache.wicket.markup.html.form.DropDownChoice
import java.util.List
import dominio.Planta
import org.uqbar.wicket.xtend.XButton
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.uqbar.commons.model.UserException

class PlantarPage extends WebPage{
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	var JardinTableroPage mainPage
	var Fila filaAPlantar
	var Jugador jugador
	var Plantar plantar
	
	new(JardinTableroPage mainPageX, Fila filaX, Jugador jugadorX){
		mainPage = mainPageX
		filaAPlantar = filaX
		jugador = jugadorX
		plantar = new Plantar(filaAPlantar)
		
		
		val plantarForm = new Form<Plantar>("plantarForm", plantar.asCompoundModel)
		
		
		listarPlantas(plantarForm)
		this.addChild(plantarForm)
		this.addChild(new FeedbackPanel("feedbackPanel"))
	}
	
	def listarPlantas(Form<Plantar> form) {
		
		form.addChild(new DropDownChoice<Planta>("plantaAPlantar") => [
			choices = loadableModel([| (this.jugador.plantas as List<Planta>)])
			choiceRenderer = choiceRenderer([Planta m| m.getNombre()])
		]) 
			 val plantarButton = new XButton("Plantar")
			 plantarButton.onClick = [|this.plantarla]
			 
//			plantarButton.add([validatable |
//			filaAPlantar.validarTipo(plantar.plantaAPlantar)])
		form.addChild(plantarButton)
		form.addChild(new XButton("Volver") => [
			onClick = [|this.volver()]])
		}
	
	def volver() {
		responsePage = mainPage
	}
	def plantarla(){
		try{
			plantar.agregar()
			this.mensajeOk()
		}
		catch(UserException e){
			error(e.getMessage())
		}
		catch(NullPointerException e){
			error("Selecciona una planta a plantar")
		}
		
	}
	
	def mensajeOk() {
		info("Plantaste la planta " + plantar.plantaAPlantar.nombre + " en la fila" )
	}
	
	}